﻿using demo1.Module.Dto;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace demo1.Controllers
{
    [ApiController]
    [Route("[controller]/[Action]")]
    public class Auth:ControllerBase
    {
        SqlSugarClient _db;
        public Auth(SqlSugarClient db) {
          _db = db;
        }

        [HttpPost]
        public string Login(UserInfo user)
        {
            return "成功";
        }
        [HttpGet]
        public async  Task<List<UserInfo>> GetUser([FromQuery] string? username) { 
        
         var user=await _db.Queryable<UserInfo>().Where(u=>u.Name==username).ToListAsync();
            return user;
        }
        [HttpPost]
        public string AddUser(UserInfo user)
        {
            var id=Guid.NewGuid().ToString("N");
            var newUser = new UserInfo() { 
            Id=id,
            Name=user.Name,
            Age=user.Age,
                Password=user.Password,
            };
            var res=_db.Insertable<UserInfo>(newUser);
            if (res !=null)
            {
                return "新增成功";
            }
            else
            {
                return "新增失败";
            }

        }
        [HttpPut]
        public string PutUser(UserInfo user) { 
        
            var res=_db.Updateable<UserInfo>(user);
            if (res != null) {
                return "修改成工";
            }
            else
            {
                return "修改是失败";
            }
        }
        [HttpDelete]
        public string DelUser([FromQuery] string id)
        {
            var res = _db.Deleteable<UserInfo>(id);
            if (res != null)
            {
                return "删除成工";
            }
            else
            {
                return "删除是失败";
            }
        }
    }
}
